//fixed firebug to ie
if(console==undefined){
	console = {};
	console.debug = console.info = log = console.error = function(){
		alert(Array.prototype.join.call(arguments, "-----"));
	}
}
var init = function(){
	
	//init the mask window
	g("_maskTitleButtonMin").onclick = function(){
		alert("Min");
	}
	g("_maskTitleButtonMax").onclick = function(){
		alert("Max");
	}
	g("_maskTitleButtonClose").onclick = function(){
		gc('mask-container')[0].style.display="none";
	}
	gc("mask-title")[0].onmousemove = function(e){
		//alert(e.button);
	}
	
	var lis = g("buttonContainer").getElementsByTagName("li");
	lis[0].onclick = function(){
		gc('mask-container')[0].style.display="block";
	}
	/*
	gc("mask-title")[0].onmousedown = function(e){
		var elementToDrag = this;
		var event = e||window.event;
		
		var deltaX=event.clientX-parseInt(elementToDrag.style.left);
		var deltaY=event.clientY-parseInt(elementToDrag.style.top);
		if(document.addEventListener){
			document.addEventListener("mousemove",moveHandler,true);
			document.addEventListener("mouseup",upHandler,true);
		}
		else if(document.attachEvent){
			document.attachEvent("onmousemove",moveHandler);
			document.attachEvent("onmouseup",upHandler);
		}
		if(event.stopPropagation){
			event.stopPropagation();
		}else{
			event.cancelBubble=true;
		}
		if(event.preventDefault){event.preventDefault();}
		else{event.returnValue=false;}
		
		
		function moveHandler(e){
			//如果是IE的事件对象，那么就用window.event
		　　if (!e) e=window.event; 
	　　	//全局属性，否则就用DOM二级标准的Event对象。
			elementToDrag.style.left=(event.clientX-deltaX)+"px";
			elementToDrag.style.top=(event.clientY-deltaY)+"px";
			if(event.stopPropagation){event.stopPropagation();}
			else{event.cancelBubble=true;}
		}
		function upHandler(e){
			if(document.removeEventListener){
				document.removeEventListener("mouseup",upHandler,true);
				document.removeEventListener("mousemove",moveHandler,true);
			}else{
				document.detachEvent("onmouseup",upHandler);
				document.detachEvent("onmousemove",moveHandler);
			}
			if(event.stopPropagation){ 
				event.stopPropagation();
			}else{
				event.cancelBubble=true;
			} 
			if(e.button==0){
				this.innerHTML+=1;
			}
		}
	}*/
	// gc("mask-title")[0].onmouseover = function(e){
		// var e = e||window.event;
		// console.debug(e.pageX+"---"+e.pageY);
	// }
	
	//gc('mask-container')[0]
	document.onmousemove = mouseMove;
	document.onmousedown = mouseDown;
	document.onmouseup   = mouseUp;
	makeDraggable(gc("mask-title")[0]);
}



function g(id){return document.getElementById(id);}
function gc(theClassName){
	if(document.getElementsByClassName){ //FF
		return document.getElementsByClassName(theClassName);
	}else{//IE
		var i=0, divs = gt("div"),res = [];
		for(var i=0; i<divs.length;i++){
			if(divs[i].className==theClassName){
				res.push(divs[i]);
			}
		}
		return res;
	}
}
function gt(tagName){return document.getElementsByTagName(tagName);}



///////////////////////////////////////////////////////////////////////////////


Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
var iMouseDown  = false;
var dragObject  = null;
var curTarget   = null;

function makeDraggable(item){
	if(!item) return;
	item.onmousedown = function(ev){
		dragObject  = this;
		mouseOffset = getMouseOffset(this, ev);
		return false;
	}
}

function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function getPosition(e){
	var left = 0;
	var top  = 0;
	while (e.offsetParent){
		left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		e     = e.offsetParent;
	}

	left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);

	return {x:left, y:top};

}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function mouseDown(ev){
	ev         = ev || window.event;
	var target = ev.target || ev.srcElement;

	if(target.onmousedown || target.getAttribute('DragObj')){
		return false;
	}
}

function mouseUp(ev){

	dragObject = null;

	iMouseDown = false;
}


function mouseMove(ev){
	ev         = ev || window.event;

	/*
	We are setting target to whatever item the mouse is currently on

	Firefox uses event.target here, MSIE uses event.srcElement
	*/
	var target   = ev.target || ev.srcElement;
	var mousePos = mouseCoords(ev);

	if(dragObject){
		dragObject.style.position = 'absolute';
		dragObject.style.top      = mousePos.y - mouseOffset.y;
		dragObject.style.left     = mousePos.x - mouseOffset.x;
	}

	// track the current mouse state so we can compare against it next time
	lMouseState = iMouseDown;

	// this prevents items on the page from being highlighted while dragging
	if(curTarget || dragObject) return false;
}

// document.onmousemove = mouseMove;
// document.onmousedown = mouseDown;
// document.onmouseup   = mouseUp;




///////////////////////////////////////////////////////////////////////////////

window.onload = init;